using System.Collections.Generic;
using GameLogic.AbilitySystem.Attribute;
using GameLogic.AbilitySystem.Effect;
using System.Linq;
using UnityEditor.Analytics;

namespace GameLogic.AbilitySystem
{
    public static class AbilitySystemExtension
    {
        #region 数值集的三种作用方式

        /// <summary>
        /// 在数值集上施加Effect
        /// 只添加damage和基础属性值，不添加buff效果
        /// </summary>
        /// <param name="instanceEffectAttribute"></param>
        public static void InstanceApply(this AttributeSet attributeSet, EffectAttribute instanceEffectAttribute)
        {
            attributeSet.Modify(instanceEffectAttribute.Modifiers);
        }

        /// <summary>
        /// 融合两个EffectAttributeSet
        /// </summary>
        /// <param name="lastingEffectAttribute"></param>
        /// <param name="other"></param>
        public static void Merge(this LastingEffectAttribute lastingEffectAttribute, LastingEffectAttribute other)
        {
            foreach (var i in other.EffectAttributes)
            {
                if (lastingEffectAttribute.EffectAttributes.ContainsKey(i.Key))
                {
                    var selFunction = lastingEffectAttribute.EffectAttributes[i.Key].function;
                    var otherFunction = i.Value.function;
                    selFunction.intercept += otherFunction.intercept;
                    selFunction.slope += otherFunction.slope - 1;
                }
            }
        }

        /// <summary>
        /// 在数值集上施加Effect
        /// 只添加damage和基础属性值，不添加buff效果
        /// </summary>
        /// <param name="attributeSet"></param>
        /// <param name="lastingEffectLastingEffectAttribute"></param>
        public static void ApplyLasting(this AttributeSet attributeSet, LastingEffectAttribute lastingEffectLastingEffectAttribute)
        {
            attributeSet.ModifyBuff(lastingEffectLastingEffectAttribute.EffectAttributes);
        }

        /// <summary>
        /// 由数值集转为修改器
        /// </summary>
        /// <param name="self"></param>
        /// <param name="mul"></param>
        /// <returns></returns>
        public static List<AttributeModifier> ToAttributeModifiers(this AttributeSet self, float mul = 1.0f)
        {
            List<AttributeModifier> retVal = new();
            for (var index = 0; index < self.Attributes.Count; index++)
            {
                var i = self.Attributes[index];
                AttributeModifier modifier = new()
                {
                    type = (AttributeType)index,
                    targetType = TargetAttributeType.Current,
                    Operator = AttributeOperator.Add,
                    ModifyValue = mul * i.CurrentValue
                };
                retVal.Add(modifier);
            }
            return retVal;
        }


        #endregion


    }
}
